import Notify from '../../miniprogram_npm/vant-weapp/notify/notify';
wx.cloud.init()
const db = wx.cloud.database({});
Page({
  data: {
    value: '',
    active: 1,
    
    tabs: ['关键词','校级','院级','佛山校区','广州校区','社联协会','志愿服务','组织策划','技能学习','体育运动','学术交流','宣传制作','音乐舞蹈','行政办公','实践DIY'],
    searchList: [],
    tagList: []
  },
  onChange(e) {
    this.setData({
      value: e.detail
    });
  },
  onSearch() {
    wx.showToast({
      icon: "loading",
      title: '搜索' + this.data.value
    })
  },
  onClick() {
    wx.showToast({
      icon: "loading",
      title: '搜索' + this.data.value
    })
  },
  changetab(event) {
    this.setData({
      active: event.detail.index
    })
    
    this.getTagList()
   
  },
  toDetail(event) {
    console.log(event.currentTarget.dataset.leguedetail._id)
    // wx.cloud.callFunction({
    //   name: 'getLegueById',
    //   data: {
    //     _id: event.currentTarget.dataset.leguedetail._id
    //   },
    //   complete: res => {
    //     console.log(res)
    //     wx.cloud.callFunction({
    //       name: 'updateLike',
    //       data :{
    //         _id: res.result.data[0]._id,
    //         like: res.result.data[0].like + 1
    //       },
    //       complete: res => {
    //         this.getTagList()
    //       }
    //     })
    //   }
    // })
    let legueDetail = JSON.stringify(event.currentTarget.dataset.leguedetail)
    
    wx.navigateTo({
      url: '../legue/leguedetail?legueDetail=' + legueDetail,
    })
  },
  onShareAppMessage: function () {
    return {
        title: '广财小站-社团推荐',
        path: '/pages/legue/legueintroduction',
        success: function (res) {
        }
    }
  },
  getTagList() {
    wx.showLoading({
      title: '加载中....',
    })
  
    wx.cloud.callFunction({
      name: 'getLegueTag',
      data :{
        tag: this.data.tabs[this.data.active],
        active: this.data.active,
        value: this.data.value
      },
      complete: res => {
        wx.hideLoading()
        function sortLike(a,b){  
          return b.like-a.like 
        }
        this.setData({
          tagList: res.result.data.sort(sortLike)
        })
        if(this.data.active == 0) {
          if(this.data.value == '' || this.data.tagList.length == 0) {
            this.setData({
              tagList: []
            })
            wx.showToast({
              icon:"none",
              title: '输入关键词试试看',
            })
          }
        }
      }
    })
  },
  
  search() {
    this.setData({
      active: 0
    })
    this.getTagList()
  },
  
  onLoad: function (options) {
    this.getTagList()
  },

  onShow: function(options) {
    this.getTagList()
  }
}); 

